﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XXN.Model;
using XXN.Model.Model;

namespace XXN.Service
{
   public class GuideService
    {
        Shopping_MallEntities db = new Shopping_MallEntities();
        public List<GuideModel> GetGuide(int page, int limit, string productName, out int count)
        {
            var pro = from item in db.Guides.Where(p => true)
                      select new GuideModel
                      {
                          Guide_id = item.Guide_id,
                          Img_id = item.Img_id,
                          Img_Url = item.IimgInfo.Url,
                          Pro_id = item.Pro_id,
                          Pro_name = item.Product.Pro_name,
                          Starttime = item.Starttime,
                          Endtime = item.Endtime,
                          Guide_Price = item.Guide_Price,
                          Guide_num = item.Guide_num
                      };
            count = pro.Count();
            if (!string.IsNullOrWhiteSpace(productName))
            {
                pro = pro.Where(p => p.Pro_name.Contains(productName));
            }
            pro = pro.OrderBy(p => p.Pro_id);
            pro = pro.Skip((page - 1) * limit).Take(limit);
            return pro.ToList();
        }
        public int AddInfo(Guide guide)
        {
            db.Guides.Add(guide);
            return db.SaveChanges();
        }
        public GuideModel EditInfo(int guide_id)
        {
            var pro = from item in db.Guides.Where(p => p.Pro_id == guide_id)
                      select new GuideModel
                      {
                          Guide_id = item.Guide_id,
                          Img_id = item.Img_id,
                          Img_Url = item.IimgInfo.Url,
                          Pro_id = item.Pro_id,
                          Pro_name = item.Product.Pro_name,
                          Starttime = item.Starttime,
                          Endtime = item.Endtime,
                          Guide_Price = item.Guide_Price,
                          Guide_num = item.Guide_num
                      };
            return pro.SingleOrDefault();
        }
        public int EditInfo(Guide guide)
        {
            db.Entry(guide).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }
        public int DelInfo(int guide_id)
        {
            var w=db.Guides.SingleOrDefault(x => x.Guide_id == guide_id);
            db.Guides.Remove(w);
            return db.SaveChanges();
        }
      /*  public List<IimgInfo> GetGuideImgTypeInfo()
        {
            return db.IimgInfoes.ToList();
        }*/
        public List<Product> GetGuideProductTypeInfo()
        {
            return db.Products.ToList();
        }
    }
}
